package com.bawei.lib_net.test_rertrofit

import android.util.Log
import retrofit2.http.GET
import retrofit2.http.Query
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Parameter
import java.lang.reflect.Proxy

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/23
 */
class RetrofitDemo2 {



    fun <T> create(service:Class<T>):T{
        val handler = object :InvocationHandler{
            override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any {
                // method 方法
                var url =  ""
                var query = ""

                method?.annotations?.forEach { annotation ->
                    when(annotation){
                        is GET -> {
                            // 获取注解的参数
                            url = annotation.value
                            println( "url: "+url)
                        }
                    }
                }

                method?.parameters?.forEach { parameter ->
                    println("name: ${parameter.name}")
                    println("type: ${parameter.type}")
                    parameter.annotations.forEach {annotation ->
                        when(annotation){
                            is Query -> {
                                query = annotation.value
                                println("Query: ${annotation.value}")
                            }
                        }
                    }
                }

                println("args: $args")
                //构建reequest对象



                //args 实参

                return method!!.invoke(this, args)
            }

        }

        return Proxy.newProxyInstance(
            service.classLoader,
            arrayOf(service),
            handler
        ) as T

    }

}